<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
mb_internal_encoding("UTF-8");
date_default_timezone_set("Asia/Chongqing");
set_time_limit(600);
chdir("/data/www/w.mdeve.com/htdocs/");

require 'runtime.php';

$index = intval(filter_input(INPUT_GET, "d", FILTER_SANITIZE_NUMBER_INT));
if(!$index) exit;
$i = ($index-1)*10;

$db = init_db();

$row = $db->get_rows("select id,cityid,url from weather_aqi limit {$i},10");
// 抓取pm2.5网页数据
foreach($row as $r){
	$data = get_file("http://www.cnpm25.cn/". $r["url"]);
	$data = skip_to_str($data, "var jin_value");
	$data = skip_to_str($data, "jin_value = \"");
	$aqi_all = get_front_str($data, "\"");
	if(false===strpos($data, "更新时间：")){
		$detail = array();
		$update_time = null;
	}else{
		$data = skip_to_str($data, "更新时间：");
		$update_time = get_front_str($data, "<");
		$data = skip_to_str($data, "</thead>");
		$data = get_front_str($data, "</table>");
	
		for($i=0;;$i++){
			if(false===strpos($data, "<tr >")) break;
			$detail = array();
			$data = skip_to_str($data, "\">");
			$name = clear_html(get_front_str($data, "</a>"));		// 监测站点
			$data = skip_to_str($data, "<td>");
			$aqi = get_front_str($data, "<");	// AQI
			$data = skip_to_str($data, "<td>");
			$data = skip_to_str($data, "<td>");
			$pm25 = get_front_str($data, "<");	// PM2.5浓度
			$data = skip_to_str($data, "<td>");
			$pm10 = get_front_str($data, "<");	// PM10浓度
			$data = skip_to_str($data, "<td>");
			$wr = get_front_str($data, "</td>");		// 首要污染物
			if(false!==strpos($wr, "<")){
				$wr = skip_to_str($wr, ">");
				$wr = get_front_str($wr, "<");
			}
	
			$detail[] = array(
					"name" => $name,
					"aqi" => $aqi,
					"pm25" => $pm25,
					"pm10" => $pm10,
					"wr" => $wr
			);
			$data = skip_to_str($data, "</tr>");
		}
	}
	$detail = base64_encode(serialize($detail));
	$db->execute("update weather_aqi set aqi={$aqi_all},detail='$detail',update_time='{$update_time}' where cityid=$r[cityid]");
}

echo "done";

?>